/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JavaAssignment;


import javax.swing.*;
import java.io.*;


/**
 *
 * @author
 */
public class Main implements Serializable{

    static GameCollection gList = new GameCollection();
    static GameCollection dataList = new GameCollection();
    static LinkList shopCart = new LinkList();
    static LinkList reviewList = new LinkList();
    static LinkList trCollection = new LinkList();

    static double maxAmt = 0;
    static double totalGamePrice;
    static int numOfGames = 0;
    static int j = 0;
    static int pagecount = 0;
    static int index = 0;
    static String gprice;
    static int serial;


    public static void main(String[] args) throws Exception {
        loadData();
        new MainPageGUI().setVisible(true);
    }

    public static void addRecord(String sID, LinkList shopCart){
        trCollection.addLast(new TransactionRecord(sID, shopCart));
    }

    public static void loadData() throws IOException {
        DataManager dm = new DataManager();
        gList = dm.loadData("gameInfo.txt");
    }

    public static void addGame() {

        String gName = JOptionPane.showInputDialog(null, "Enter Name: ");
        String gDesc = JOptionPane.showInputDialog(null, "Enter Description: ");
        String gCat = JOptionPane.showInputDialog(null, "Enter Category: ");
        gprice = JOptionPane.showInputDialog(null, "Enter Price:");
        containsOnlyNumbers(gprice);
        double gPrice = Double.parseDouble(gprice);
        int gRevno = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Review No: "));
        for (int x = 0; x < gRevno; x++) {
            String gameReviewMag = JOptionPane.showInputDialog(null, "Enter game review magazine: ");
            String gameReviewRating = JOptionPane.showInputDialog(null, "Enter Review Rating: ");
            reviewList.addLast(new GameReview(gameReviewMag, gameReviewRating));
        }
        String gImage = JOptionPane.showInputDialog(null, "Enter Image: ", "");
        gList.addGame((new GameData(gName, gDesc, gCat, gPrice, gRevno, reviewList, gImage)));
    }

    public static boolean containsOnlyNumbers(String p) {
        boolean abc = false;
        gprice = p;
        //It can't contain only numbers if it's null or empty...
        while (abc == false) {
            abc = true;
            if (gprice == null || gprice.length() == 0) {
                JOptionPane.showMessageDialog(null, "Text field is empty", "Error", JOptionPane.ERROR_MESSAGE);
                gprice = JOptionPane.showInputDialog(null, "Enter Price: ");

                abc = false;
            } else {


                for (int i = 0; i < gprice.length(); i++) {
                    //If we find a non-digit character we return false.
                    if (Character.isDigit(gprice.charAt(i)) == false) {
                        //If we find a non-digit character we 
                        JOptionPane.showMessageDialog(null, "Text field contains alphabets", "Error", JOptionPane.ERROR_MESSAGE);
                        //JOptionPane.showMessageDialog(null, "the" + i + "DIGIT IS" + gprice.charAt(i));
                        gprice = JOptionPane.showInputDialog(null, "Enter Price: ");
                        abc = false;

                    }
                }

                for (int i = 0; i < gprice.length(); i++) {
                    //If we find a non-digit character we return false.
                    if (Character.isDigit(gprice.charAt(i)) == false) {
                        //If we find a non-digit character we
                        JOptionPane.showMessageDialog(null, "Text field contains alphabets", "Error", JOptionPane.ERROR_MESSAGE);
                        // JOptionPane.showMessageDialog(null,"the"+i+"DIGIT IS"+gprice.charAt(i));
                        gprice = JOptionPane.showInputDialog(null, "Enter Price: ");
                        abc = false;
                    }
                }
            }
        }
        return abc;
    }
}
   

